<template>
  <div class="app-container">
    <el-card>
      <!-- <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择状态"
          clearable
        >
          <el-option
            v-for="dict in dict.type.sys_normal_disable"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form> -->

      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            >新增</el-button
          >
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="success"
            plain
            icon="el-icon-edit"
            size="mini"
            :disabled="single"
            @click="handleUpdate"
            >修改</el-button
          >
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
            >删除</el-button
          >
        </el-col>
        <!-- <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
            v-hasPermi="['school:show:export']"
            >导出</el-button
          >
        </el-col> -->
        <right-toolbar
          :showSearch.sync="showSearch"
          @queryTable="getList"
        ></right-toolbar>
      </el-row>

      <el-table
        v-loading="loading"
        :data="showList"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" align="center" />
        <!-- <el-table-column
          label="序号"
          align="center"
          prop="schoolId"
          v-if="true"
        /> -->
        <el-table-column
          label="序号"
          type="index"
          :index="table_index"
          width="50"
          align="center"
        >
        </el-table-column>
        <el-table-column
          label="学校图片"
          align="center"
          prop="schoolPicture"
          width="100"
        >
          <template slot-scope="scope">
            <image-preview
              :src="scope.row.schoolUrl"
              :width="50"
              :height="50"
            />
          </template>
        </el-table-column>
        <el-table-column label="排序" align="center" prop="orderBy" />
        <el-table-column label="状态" align="center" prop="status">
          <template slot-scope="scope">
            <dict-tag
              :options="dict.type.sys_normal_disable"
              :value="scope.row.status"
            />
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          align="center"
          class-name="small-padding fixed-width"
        >
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              >修改</el-button
            >
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />

      <!-- 添加或修改学校介绍管理对话框 -->
      <el-dialog
        :title="title"
        :visible.sync="open"
        width="500px"
        append-to-body
      >
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
          <el-form-item label="学校图片">
            <image-upload v-model="form.schoolPicture" :limit="1" />
          </el-form-item>
          <el-form-item label="显示排序" prop="orderBy">
            <el-input-number
              v-model="form.orderBy"
              controls-position="right"
              :min="0"
            />
          </el-form-item>
          <el-form-item label="状态">
            <el-radio-group v-model="form.status">
              <el-radio
                v-for="dict in dict.type.sys_normal_disable"
                :key="dict.value"
                :label="dict.value"
                >{{ dict.label }}</el-radio
              >
            </el-radio-group>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button :loading="buttonLoading" type="primary" @click="submitForm"
            >确 定</el-button
          >
          <el-button @click="cancel">取 消</el-button>
        </div>
      </el-dialog>
    </el-card>
    <el-card style="margin-top: 20px">
      <div>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              plain
              icon="el-icon-plus"
              size="mini"
              @click="handleAdds"
              >新增</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="success"
              plain
              icon="el-icon-edit"
              size="mini"
              :disabled="singles"
              @click="handleUpdates"
              >修改</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="danger"
              plain
              icon="el-icon-delete"
              size="mini"
              :disabled="multiples"
              @click="handleDeletes"
              >删除</el-button
            >
          </el-col>
          <!-- <el-col :span="1.5">
            <el-button
              type="warning"
              plain
              icon="el-icon-download"
              size="mini"
              @click="handleExports"
              v-hasPermi="['palmary:show:export']"
              >导出</el-button
            >
          </el-col> -->
          <right-toolbar
            :showSearch.sync="showSearchs"
            @queryTable="getLists"
          ></right-toolbar>
        </el-row>

        <el-table
          v-loading="loading"
          :data="showLists"
          @selection-change="handleSelectionChanges"
        >
          <el-table-column type="selection" width="55" align="center" />
          <!-- <el-table-column
            label="序号"
            align="center"
            prop="palmaryId"
            v-if="true"
          /> -->
          <el-table-column
            label="序号"
            type="index"
            :index="table_index"
            width="50"
            align="center"
          >
          </el-table-column>
          <el-table-column
            label="标题名称"
            align="center"
            prop="palmaryTitle"
          />
          <el-table-column
            label="标题内容"
            align="center"
            prop="palmaryContent"
          >
            <template slot-scope="scope">
              <div v-html="scope.row.palmaryContent"></div>
            </template>
          </el-table-column>
          <el-table-column label="排序" align="center" prop="orderBy" />
          <el-table-column label="状态" align="center" prop="status">
            <template slot-scope="scope">
              <dict-tag
                :options="dict.type.sys_normal_disable"
                :value="scope.row.status"
              />
            </template>
          </el-table-column>
          <el-table-column
            label="操作"
            align="center"
            class-name="small-padding fixed-width"
          >
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdates(scope.row)"
                >修改</el-button
              >
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDeletes(scope.row)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total > 0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getLists"
        />

        <!-- 添加或修改学校介绍管理对话框 -->
        <el-dialog
          :title="title"
          :visible.sync="opens"
          width="600px"
          append-to-body
        >
          <el-form ref="forms" :model="forms" :rules="rules" label-width="80px">
            <el-form-item label="标题名称" prop="palmaryTitle">
              <el-input
                v-model="forms.palmaryTitle"
                placeholder="请输入标题名称"
              />
            </el-form-item>
            <el-form-item label="标题内容">
              <el-input
                id="input1"
                type="textarea"
                v-model="forms.palmaryContent"
                placeholder="请输入标题内容"
                maxlength="300"
                show-word-limit
                style="100%"
              />
              <!-- <el-input
                v-model="forms.palmaryContent"
                placeholder="请输入标题内容"
              /> -->
            </el-form-item>
            <el-form-item label="显示排序" prop="orderBy">
              <el-input-number
                v-model="forms.orderBy"
                controls-position="right"
                :min="0"
              />
            </el-form-item>
            <el-form-item label="状态">
              <el-radio-group v-model="forms.status">
                <el-radio
                  v-for="dict in dict.type.sys_normal_disable"
                  :key="dict.value"
                  :label="dict.value"
                  >{{ dict.label }}</el-radio
                >
              </el-radio-group>
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button
              :loading="buttonLoading"
              type="primary"
              @click="submitForms"
              >确 定</el-button
            >
            <el-button @click="cancels">取 消</el-button>
          </div>
        </el-dialog>
      </div>
    </el-card>
  </div>
</template>
  
  <script>
import {
  listShow,
  getShow,
  delShow,
  addShow,
  updateShow,
} from "@/api/school/show";
import {
  listShows,
  getShows,
  delShows,
  addShows,
  updateShows,
} from "@/api/palmary/show";

export default {
  name: "Show",
  dicts: ["sys_normal_disable"],
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 选中数组
      idss: [],
      // 非单个禁用
      singles: true,
      // 非多个禁用
      multiples: true,
      // 显示搜索条件
      showSearch: true,
      // 显示搜索条件
      showSearchs: true,
      // 总条数
      total: 0,
      // 学校介绍管理表格数据
      showList: [],
      // 学校介绍管理表格数据
      showLists: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否显示弹出层
      opens: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        status: undefined,
      },
      // 表单参数
      form: {},
      // 表单参数
      forms: {},
      rules: {
        palmaryTitle: [
          { required: true, message: "标题名称不能为空", trigger: "blur" },
        ],
        palmaryContent: [
          { required: true, message: "标题内容不能为空", trigger: "blur" },
        ],
        schoolPicture: [
          { required: true, message: "学校图片不能为空", trigger: "blur" },
        ],
        status: [{ required: true, message: "状态不能为空", trigger: "blur" }],
      },
    };
  },
  created() {
    this.getList();
    this.getLists();
  },
  methods: {
    //序号换页问题
    table_index(index) {
      return (
        (this.queryParams.pageNum - 1) * this.queryParams.pageSize + index + 1
      );
    },
    /** 查询学校介绍管理列表 */
    getList() {
      this.loading = true;
      listShow(this.queryParams).then((response) => {
        this.showList = response.rows;
        console.log(this.showList);
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询学校介绍管理列表 */
    getLists() {
      this.loading = true;
      listShows(this.queryParams).then((response) => {
        this.showLists = response.rows;

        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 取消按钮
    cancels() {
      this.opens = false;
      this.resets();
    },
    // 表单重置
    reset() {
      this.form = {
        schoolId: undefined,
        schoolPicture: undefined,
        schoolUrl: undefined,
        status: "0",
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        remark: undefined,
        isDelete: undefined,
        orderBy: 0,
      };
      this.resetForm("form");
    },
    // 表单重置(内容)
    resets() {
      this.forms = {
        palmaryId: undefined,
        palmaryTitle: undefined,
        palmaryContent: undefined,
        status: "0",
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        remark: undefined,
        isDelete: undefined,
        orderBy: 0,
      };
      this.resetForm("forms");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 搜索按钮操作(内容) */
    handleQuerys() {
      this.queryParams.pageNum = 1;
      this.getLists();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 重置按钮操作(内容) */
    resetQuerys() {
      this.resetForms("queryForms");
      this.handleQuerys();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.schoolId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    // 多选框选中数据
    handleSelectionChanges(selection) {
      this.idss = selection.map((item) => item.palmaryId);
      this.singles = selection.length !== 1;
      this.multiples = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加学校介绍管理";
    },
    /** 新增按钮操作 */
    handleAdds() {
      this.resets();
      this.opens = true;
      this.title = "添加学校介绍管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const schoolId = row.schoolId || this.ids;
      getShow(schoolId).then((response) => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改学校介绍管理";
      });
    },
    /** 修改按钮操作 */
    handleUpdates(row) {
      this.loading = true;
      this.reset();
      const palmaryId = row.palmaryId || this.idss;
      getShows(palmaryId).then((response) => {
        this.loading = false;
        this.forms = response.data;
        this.opens = true;
        this.title = "修改学校介绍管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.schoolId != null) {
            updateShow(this.form)
              .then((response) => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              })
              .finally(() => {
                this.buttonLoading = false;
              });
          } else {
            addShow(this.form)
              .then((response) => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              })
              .finally(() => {
                this.buttonLoading = false;
              });
          }
        }
      });
    },
    /** 提交按钮 */
    submitForms() {
      this.$refs["forms"].validate((valid) => {
        if (valid) {
          this.buttonLoading = true;
          if (this.forms.palmaryId != null) {
            updateShows(this.forms)
              .then((response) => {
                this.$modal.msgSuccess("修改成功");
                this.opens = false;
                this.getLists();
              })
              .finally(() => {
                this.buttonLoading = false;
              });
          } else {
            addShows(this.forms)
              .then((response) => {
                this.$modal.msgSuccess("新增成功");
                this.opens = false;
                this.getLists();
              })
              .finally(() => {
                this.buttonLoading = false;
              });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const schoolIds = row.schoolId || this.ids;
      this.$modal
        .confirm('是否确认删除学校介绍管理编号为"' + schoolIds + '"的数据项？')
        .then(() => {
          this.loading = true;
          return delShow(schoolIds);
        })
        .then(() => {
          this.loading = false;
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {})
        .finally(() => {
          this.loading = false;
        });
    },
    /** 删除按钮操作 */
    handleDeletes(row) {
      const palmaryIds = row.palmaryId || this.idss;
      this.$modal
        .confirm('是否确认删除学校介绍管理编号为"' + palmaryIds + '"的数据项？')
        .then(() => {
          this.loading = true;
          return delShows(palmaryIds);
        })
        .then(() => {
          this.loading = false;
          this.getLists();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {})
        .finally(() => {
          this.loading = false;
        });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "school/show/export",
        {
          ...this.queryParams,
        },
        `show_${new Date().getTime()}.xlsx`
      );
    },
    /** 导出按钮操作 */
    handleExports() {
      this.download(
        "palmary/show/export",
        {
          ...this.queryParams,
        },
        `show_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
<style>
.demo-image__lazy {
  height: 600px;
  overflow-y: auto;
}
.demo-image__lazy .el-image {
  display: block;
  min-height: 200px;
  margin-bottom: 10px;
}
.demo-image__lazy .el-image:last-child {
  margin-bottom: 0;
}
.el-dialog {
  display: flex;
  flex-direction: column;
  margin: 0 !important;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  max-height: calc(100% - 30px);
  max-width: calc(100% - 30px);
}
.content {
  text-indent: 2em;
}
</style>